package com.springmvc.cto51.controller;

import com.springmvc.cto51.vo.UserInfo;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * UserController--SpringMVC-test
 *
 * @author: Jovi
 * @createTime: 2018-08-16 16:07
 **/
@Controller
@RequestMapping("/user")
public class UserController {

    @RequestMapping(value="/list.do")
    public String list(){
        System.out.println("------查询用户信息------");
        return "cto51/userInfo/user_list";
    }

    @RequestMapping(value="/add.do")
    public String add(){
        System.out.println("------用户添加-------");
        return "cto51/userInfo/user_add";
    }

    @RequestMapping(value="/update.do")
    public String update(){
        System.out.println("------用户修改-------");
        return "cto51/userInfo/user_update";
    }

    /**
     * 直接使用request.setAttribute返回页面数据
     * @param req
     * @return
     */
    @RequestMapping(value = "/add1.do")
    public String add(HttpServletRequest req) {
        String userName = req.getParameter("userName");
        String password = req.getParameter("password");
        System.out.println("userName" + userName + "\n" + "password" + password);

        String user = "userName" + userName + "\n" + "password" + password;

        req.setAttribute("user", user);

        return "cto51/userInfo/user_add1";
    }

    @RequestMapping(value = "/add2.do")
    public ModelAndView add2() {
        ModelAndView mav = new ModelAndView("cto51/userInfo/user_add2");
        mav.addObject("userId", 1001);
        mav.addObject("userName", "aaa");
        return mav;
    }

    @RequestMapping(value = "/add3.do")
    public String add3(Model model) {
        model.addAttribute("personId", 95218);
        model.addAttribute("personName", "tang1");
        return "cto51/userInfo/user_add3";
    }

    @RequestMapping(value = "/add4.do")
    public String add4(Map map) {
        map.put("personId", 95218);
        map.put("personName", "tang2");
        return "cto51/userInfo/user_add4";
    }

    /**
     * 这里必须使用@InitBinder时间属性编辑器，
     * 如果是pojo类里的则可以使用注解@DateTimeFormat(pattern="yyyy-MM-dd")
     * @param id
     * @param userTime
     * @return
     */
    @RequestMapping(value="/add5.do")
    public String add5(Integer id,Date userTime){
        System.out.println(id);
        System.out.println(userTime);
        return "cto51/userInfo/user_add5";
    }

    @RequestMapping(value = "/add6.do")
    public String add6(@RequestParam(value = "name") String userName, String password) {
        System.out.println("userName" + userName + "\n" + "password" + password);

        return "cto51/userInfo/user_add6";
    }

    /**
     * 通过对象这种方式必须手写getset方法不能用注解，表单的name属性的值必须和类的属性名一致
     * @param userInfo
     * @return
     */
    @RequestMapping(value = "/add7.do")
    public String add7(UserInfo userInfo) {
        System.out.println("userInfo" + userInfo.toString() );
        return "cto51/userInfo/user_add7";
    }

    @InitBinder
    public void initBinder(ServletRequestDataBinder bin){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        CustomDateEditor cust = new CustomDateEditor(sdf,true);
        bin.registerCustomEditor(Date.class,cust);
    }

}

